import { getToken, setToken } from "../utils/auth";
import { defineStore } from "pinia";
import { ref } from "vue";
import { login as loginApi } from "../api/user";

export const useUserStore = defineStore("user", () => {
  const token = ref("");
  token.value = getToken() ?? "";
  async function login(userInfo: any) {
    const { username, password } = userInfo;
    const { data } = await loginApi({ username: username.trim(), password });
    const tokenStr = data.tokenHead + data.token;
    // 存储 token 到 cookie 中
    setToken(tokenStr);
    token.value = tokenStr;
  }
  return {
    login,
    token,
  };
});
